package cn.edu.jsu.nrs.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import cn.edu.jsu.nrs.dao.UserDao;
import cn.edu.jsu.nrs.model.User;

/**
 * 
 * @author:YYH
 * @date:2013-1-24 下午08:13:43
 * @update-date:2013-1-24 下午08:13:43
 * @version:1.0
 * @project_name:NewsReleaseSystem
 * @description:用户数据访问类接口的实现
 */
@Component(value = "userDaoImpl")
public class UserDaoImpl implements UserDao {

	private HibernateTemplate hibernateTemplate;

	// 插入用户信息
	public int save(User user) {
		return (Integer) hibernateTemplate.save(user);
	}

	// 根据用户ID查询用户
	public User queryUserById(int id) {
		User user = (User) hibernateTemplate.get(User.class, id);
		return user;
	}

	// 更新用户信息
	public void update(User user) {
		hibernateTemplate.update(user);
	}

	// 删除用户
	public void delete(User user) {
		// hibernateTemplate.delete(user.getQuestion());
		hibernateTemplate.delete(user);

	}
 
	// 根据用户id删除用户
	public void deleteById(int id) {
		User user = queryUserById(id);
		this.delete(user);

	}

	/*
	 * 判断邮箱是否存在
	 */
	@SuppressWarnings("unchecked")
	public boolean isExists(String userName) {

		List<User> usersList = hibernateTemplate.find(
				"from User where userName=? and is_Admin=0", userName);
		if (usersList.size() > 0) {
			return true;
		}
		return false;
	}

	// 查找所有用户
	@SuppressWarnings("unchecked")
	public List<User> queryAllUsers() {
		return hibernateTemplate.find("from User");

	}

	// 通过用户名，密码，类型查找用户
	@SuppressWarnings("unchecked")
	public User queryUser(String userName, String password,int isAdmin) {
		User user = new User();
		Object[] values = {userName,password,isAdmin};
		List<User> users =hibernateTemplate.find("from User where userName=? and password=? and is_Admin=?",values);
		if (users.size() > 0) {
			user = (User) users.get(0);
		}
		return user;
	}

	// 查询用户的总页数
	@SuppressWarnings("unchecked")
	public long pageCount(int size) {
		List<Long> count = new ArrayList<Long>();
		count = hibernateTemplate.find("select COUNT(*)from User");
		if (count.size() > 0) {
			return (count.get(0) - 1 + size) / size;
		}
		return 0;
	}

	/***
	 * 用户分页
	 */
	@SuppressWarnings("unchecked")
	public List<User> paging(final int index, final int size,
			final String propertyName) {
		List<User> userList = hibernateTemplate.executeFind(
				new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Criteria criteria = session.createCriteria(User.class);
						if ((propertyName != null)
								&& (propertyName.length() > 0)) {
							criteria.addOrder(org.hibernate.criterion.Order
									.desc(propertyName));
						}
						criteria.setFirstResult((index - 1) * size);
						criteria.setMaxResults(size);
						return criteria.list();
					}
				});
		return userList;
	}

	@SuppressWarnings("unchecked")
	public List<User> getListForPage(final String hql, final int beginIndex,
			final int length) {
		List<User> users = hibernateTemplate.executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						query.setFirstResult(beginIndex);
						query.setMaxResults(length);
						query.setFetchSize(length);
						return query.list();
					}
				});

		return users;
	}
	
	
	
	@SuppressWarnings("unchecked")
	public List<User> queryUserList(final int beginIndex,final int count,final String userName,final String sord,final String sidx){
		
		List<User> users = hibernateTemplate.executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql="from User where isAdmin=0";
						if (userName!=null) {
							hql=hql+" and userName like '%"+userName+"%'";
						}
						if (sidx==null) {
							hql=hql+"order by userId desc";
						}else {
							hql=hql+" order by "+sidx+" "+sord;
						}
						Query query = session.createQuery(hql);
						query.setFirstResult(beginIndex);
						query.setMaxResults(count);
						return query.list();
					}
				});
		
		return users;
	}

	
	/**
	 * 根据用户模糊查询，查询用户记录条数
	 */
	public int queryUsersCounts(final String userName){
		String hql="from User where isAdmin=0";
		if (userName!=null) {
			hql=hql+" and userName like '%"+userName+"%'";
		}
		return  hibernateTemplate.find(hql).size();
	}
	
	/**
	 *  查询用户记录数
	 */
	public int queryUsersCounts() {
		return hibernateTemplate.find("from User").size();
	}
	
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
